package file.send.test01;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

public class Send {

	/** 方法名： 
	 * @author zhangjiwei
	 * 创建时间：2016-3-7
	 * @param args
	 */
	public static void main(String[] args) {
//		sendGet();
		sendFile();
	}

	/**
	 * 方法名：发送文件
	 * 参考地址：http://blog.csdn.net/zhouyingge1104/article/details/28267827 
	 * @author zhangjiwei
	 * 创建时间：2016-3-7
	 */
	static void sendFile() {
		final String boundary = "Boundary-b1ed-4060-99b9-fca7ff59c113";
//		final String boundary = "---------------------------7d33a816d302b6";
		final String enter = "\r\n";
		
		File file = new File("C:\\1452670612808563.jpg");
		try {
			FileInputStream fileInputStream = new FileInputStream(file);
			
			URL url = new URL("http://localhost:8080/fileSendTest/servlet/GetFile");
			HttpURLConnection huc = (HttpURLConnection) url.openConnection();
			huc.setDoOutput(true);
			huc.setDoInput(true);
			huc.setUseCaches(false);
			huc.setInstanceFollowRedirects(true);
			huc.setRequestMethod("POST");
			huc.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary);
			huc.connect();
			DataOutputStream dataOutputStream = new DataOutputStream(huc.getOutputStream());
			
			String part1 = "--" + boundary + enter
					+ "Content-Type:application/octet-stream" + enter
					+ "Content-Disposition:form-data;filename=\"" + file.getName() + "\";name=\"myfile\"" + enter + enter;
			String part2 = enter
					+ "--" + boundary + enter
					+ "Content-Type:text/plain" + enter
					+ "Content-Disposition:from-data;name=\"param\"" + enter + enter
					+ "hk" + enter
					+ "--" + boundary + "--";
			byte[] fileBytes = new byte[fileInputStream.available()];
			fileInputStream.read(fileBytes);
			
			dataOutputStream.writeBytes(part1);
			dataOutputStream.write(fileBytes);
			dataOutputStream.writeBytes(part2);
			
			dataOutputStream.flush();
			dataOutputStream.close();
			fileInputStream.close();
			
			System.out.println("request status code:" + huc.getResponseCode());
			
//			BufferedReader br = new BufferedReader(new InputStreamReader(huc.getInputStream()));
//			String line = "";
//			StringBuffer result = new StringBuffer();
//			while((line = br.readLine()) != null) {
//				result.append(line);
//			}
//			System.out.println(result);
			
			huc.disconnect();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	static void sendGet() {
		try {
			URL url = new URL("http://localhost:8080/fileSendTest/servlet/GetFile");
			HttpURLConnection huc = (HttpURLConnection) url.openConnection();
			huc.setDoOutput(true);
//			huc.setRequestMethod("GET");
			huc.setRequestMethod("POST");
			huc.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
			huc.setRequestProperty("Connection", "Keep-Alive");
			
			BufferedReader br = new BufferedReader(new InputStreamReader(huc.getInputStream(), "UTF-8"));
			String line = "";
			StringBuffer result = new StringBuffer();
			while((line = br.readLine()) != null) {
				result.append(line);
			}
			System.out.println(result);
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
